package com.captainbank.tcl.tclapplication.ui.views;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.captainbank.tcl.tclapplication.R;
import com.gyf.barlibrary.ImmersionBar;

/**
 * Created by TCL on 2019/4/12.
 * Describe:
 */
public abstract class BaseDialogFragment extends DialogFragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化数据
        initData(getArguments());
        // 设置自定义样式
        setStyle(setDialogStyle(), setDialogTheme());
        //设置返回键
        setCancelable(setCancel());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isImmersionBarEnabled()) {
            initImmersionBar();
        }
        initView(view);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(setCancelOnTouchOutside());
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            if(setWidth()== WindowManager.LayoutParams.MATCH_PARENT){
                int leftAndRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, setLeftRightPadding(), Resources.getSystem().getDisplayMetrics());
                window.getDecorView().setPadding(leftAndRight, 0, leftAndRight, 0);
            }
            window.setWindowAnimations(setWindowAnimationsStyle());
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.gravity = setGravity();
            wlp.dimAmount = setBgAlpha();
            wlp.width = setWidth();
            wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(wlp);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(isImmersionBarEnabled()){
            ImmersionBar.with(this).destroy();
        }
    }

    /**
     * 是否在Fragment使用沉浸式
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return false;
    }

    /**
     * 初始化沉浸式
     * * 含有输入框的子类重写此方法注意与软键盘冲突
     */
    protected void initImmersionBar() {
        ImmersionBar.with(this).init();
    }

    /**
     * 设置弹出框样式
     */
    protected int setDialogStyle() {
        return DialogFragment.STYLE_NO_TITLE;
    }

    /**
     * 设置弹出框主题
     */
    protected int setDialogTheme() {
        return R.style.Theme_AppCompat_Dialog;
    }

    /**
     * 设置返回键
     */
    protected Boolean setCancel() {
        return true;
    }

    /**
     * 设置空白区域
     */
    protected Boolean setCancelOnTouchOutside() {
        return true;
    }

    /**
     * 设置左右间距
     */
    protected int setLeftRightPadding() {
        return 0;
    }

    /**
     * 设置左右间距
     */
    protected float setBgAlpha() {
        return 0.4f;
    }

    /**
     * 设置窗口转场动画
     */
    protected int setWindowAnimationsStyle() {
        return R.style.Animation_Design_BottomSheetDialog;
    }

    /**
     * 设置窗口宽
     */
    protected int setWidth() {
        return WindowManager.LayoutParams.MATCH_PARENT;
    }

    /**
     * 设置Dialog gravity
     */
    protected int setGravity() {
        return Gravity.CENTER;
    }

    /**
     * 自定义时添加layout
     */
    protected abstract int getLayoutId();

    /**
     * 加载数据
     *
     * @param bundle 用这个Bundle对象接收传入时的Bundle对象
     */
    protected abstract void initData(Bundle bundle);

    /**
     * 初始化
     */
    protected abstract void initView(View view);
}
